/****************************************************************************
	This file is part of Basqet.

	Basqet is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Basqet is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
    along with Basqet.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************************/

#include "richtextedit.h"

#include <QtDebug>
RichTextEdit::RichTextEdit(QWidget* parent /*= 0*/)
		:QTextEdit(parent)
{

}


void RichTextEdit::focusInEvent(QFocusEvent * event)
{
	QTextEdit::focusInEvent(event);
	UICapabilities capabilities;
	capabilities.setRichText(true);

	emit activated(this, capabilities);

}


void RichTextEdit::deActivated()
{

}





// Toggle Bold in selected text.
void RichTextEdit::setBold()
{

	if (fontWeight() == QFont::Normal)
	{
		qDebug() << "RichTextEdit: setBold";
		setFontWeight(QFont::Bold);
	}
	else
	{
		qDebug() << "RichTextEdit: ClearBold";
		setFontWeight(QFont::Normal);
	}


}

// Toggle Italic on selected text.
void RichTextEdit::setItalic()
{
	setFontItalic(!fontItalic());

}

void RichTextEdit::setUnderline()
{
	setFontUnderline(!fontUnderline());
}

void RichTextEdit::setStrikethrough()
{
	// There is no "setFontStrikethrough()" in QTextEdit
	// so we have to do it with a workaround - with
	// the QTextCharFormat for the cursor selection or
	// full text.
	QTextCharFormat fmt;
	fmt.setFontStrikeOut(!currentCharFormat().fontStrikeOut());

	QTextCursor cursor = textCursor();
	QTextCursor wordStart(cursor);
	QTextCursor wordEnd(cursor);

	wordStart.movePosition(QTextCursor::StartOfWord);
	wordEnd.movePosition(QTextCursor::EndOfWord);

	cursor.beginEditBlock();
	if (!cursor.hasSelection()
	    && cursor.position() != wordStart.position()
	    && cursor.position() != wordEnd.position())
	{
		cursor.select(QTextCursor::WordUnderCursor);
	}
	cursor.mergeCharFormat(fmt);
	mergeCurrentCharFormat(fmt);
	cursor.endEditBlock();
}

void RichTextEdit::incFontSize()
{

	if (fontPointSize() < 1)
		setFontPointSize(8);
	else
		setFontPointSize(fontPointSize() + 1);

}


void RichTextEdit::decFontSize()
{
	if (fontPointSize() > 4)
		setFontPointSize(fontPointSize() - 1);

}


void RichTextEdit::setLeft()
{
    setAlignment(Qt::AlignLeft);
}


void RichTextEdit::setRight()
{
    setAlignment(Qt::AlignRight);
}


void RichTextEdit::setMid()
{
    setAlignment(Qt::AlignCenter);
}

